// ignore_for_file: prefer_interpolation_to_compose_strings, unnecessary_string_interpolations
import 'package:flutter_kit_plus/flutter_kit.dart';

const write_service = 'FFD5';
const write_uuid = 'FFD9';

const read_service = 'FFD0';
const read_uuid = 'FFD4';

class SendData {
  static List<int> getBytes({String tag = '', dynamic data1, dynamic data2, dynamic data3}) {
    List<int> bytes = [];
    switch (tag) {
      case '串口':

        ///串口数据格式： 起始符(0xCC, 0x20)2字节+数据包长度(1字节)+命令字(1字节)+数据(<240字节)+结束符0x55)1字节
        ///详细命令见文档
        List<int> header = 0xCC20.bytes;
        List<int> end = 0x55.bytes;
        List<int> data = (data1 as int).bytes;
        List<int> length = (header.length + end.length + data.length + 1).bytes;
        bytes = header + length + data + end;
      case '颜色':
        List<int> datas = (data1 as List<int>);
        bytes += [0x56];
        bytes += datas;
        bytes += [0x00, 0xAA];
      default:
        break;
    }
    return bytes;
  }

  static int crc(int cmd, List<int> value) {
    int xor = cmd ^ value.length;
    for (var byte in value) {
      xor = xor ^ byte;
    }
    return xor & 0xFF;
  }

  static void send({String tag = '', dynamic data1, dynamic data2, dynamic data3}) {
    List<int> bytes = getBytes(tag: tag, data1: data1, data2: data2, data3: data3);
    if (bytes.length <= 0) {
      return;
    }
    LogRecorder.addLog(bytes.toHexstr(' '));
    print('发送数据 = ${bytes.toHexstr(' |')}');
    // BluetoothManager.instance.write(data: bytes, characteristics_uuid: 'FAA1');
    BluetoothManager.instance.write(data: bytes, characteristics_uuid: write_uuid);
  }
}


// const String Service_FRI_UUID = 'FAA0';
// const String WriteChar_FRI_UUID = 'FAA1';
// const String ReadChar_FRI_OTA_UUID = 'FAA1';
// const String ReadChar_FRI_UUID = 'FAA1';